// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyColorTableAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <PyColorControlPointList.h>

// ****************************************************************************
// Module: PyColorTableAttributes
//
// Purpose:
//   This class contains the list of colortables.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a ColorTableAttributes.
//
struct ColorTableAttributesObject
{
    PyObject_HEAD
    ColorTableAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewColorTableAttributes(int);
std::string
PyColorTableAttributes_ToString(const ColorTableAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    {   const stringVector &colorTableNames = atts->GetColorTableNames();
        snprintf(tmpStr, 1000, "%scolorTableNames = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < colorTableNames.size(); ++i)
        {
            snprintf(tmpStr, 1000, "\"%s\"", colorTableNames[i].c_str());
            str += tmpStr;
            if(i < colorTableNames.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const intVector &colorTableActiveFlags = atts->GetColorTableActiveFlags();
        snprintf(tmpStr, 1000, "%scolorTableActiveFlags = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < colorTableActiveFlags.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", colorTableActiveFlags[i]);
            str += tmpStr;
            if(i < colorTableActiveFlags.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    { // new scope
        int index = 0;
        // Create string representation of colorTables from atts.
        for(AttributeGroupVector::const_iterator pos = atts->GetColorTables().begin(); pos != atts->GetColorTables().end(); ++pos, ++index)
        {
            const ColorControlPointList *current = (const ColorControlPointList *)(*pos);
            snprintf(tmpStr, 1000, "GetColorTables(%d).", index);
            std::string objPrefix(prefix + std::string(tmpStr));
            str += PyColorControlPointList_ToString(current, objPrefix.c_str(), forLogging);
        }
        if(index == 0)
            str += "#colorTables does not contain any ColorControlPointList objects.\n";
    }
    snprintf(tmpStr, 1000, "%sdefaultContinuous = \"%s\"\n", prefix, atts->GetDefaultContinuous().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sdefaultDiscrete = \"%s\"\n", prefix, atts->GetDefaultDiscrete().c_str());
    str += tmpStr;
    return str;
}

static PyObject *
ColorTableAttributes_Notify(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_SetColorTableNames(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;

    stringVector vec;

    if (PyUnicode_Check(args))
    {
        char const *val = PyUnicode_AsUTF8(args);
        std::string cval = std::string(val);
        if (val == 0 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ string");
        }
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyUnicode_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a unicode string", (int) i);
            }

            char const *val = PyUnicode_AsUTF8(item);
            std::string cval = std::string(val);

            if (val == 0 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ string", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more string(s)");

    obj->data->GetColorTableNames() = vec;
    // Mark the colorTableNames in the object as modified.
    obj->data->SelectColorTableNames();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_GetColorTableNames(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the colorTableNames.
    const stringVector &colorTableNames = obj->data->GetColorTableNames();
    PyObject *retval = PyTuple_New(colorTableNames.size());
    for(size_t i = 0; i < colorTableNames.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyString_FromString(colorTableNames[i].c_str()));
    return retval;
}

/*static*/ PyObject *
ColorTableAttributes_SetColorTableActiveFlags(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetColorTableActiveFlags() = vec;
    // Mark the colorTableActiveFlags in the object as modified.
    obj->data->SelectColorTableActiveFlags();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_GetColorTableActiveFlags(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the colorTableActiveFlags.
    const intVector &colorTableActiveFlags = obj->data->GetColorTableActiveFlags();
    PyObject *retval = PyTuple_New(colorTableActiveFlags.size());
    for(size_t i = 0; i < colorTableActiveFlags.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(colorTableActiveFlags[i])));
    return retval;
}

/*static*/ PyObject *
ColorTableAttributes_GetColorTables(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    int index = -1;
    if (args == NULL)
        return PyErr_Format(PyExc_NameError, "Use .GetColorTables(int index) to get a single entry");
    if (!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "arg must be a single integer index");
    if (index < 0 || (size_t)index >= obj->data->GetColorTables().size())
        return PyErr_Format(PyExc_ValueError, "index out of range");

    // Since the new object will point to data owned by the this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyColorControlPointList_Wrap(&obj->data->GetColorTables(index));
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyColorControlPointList_SetParent(retval, self);

    return retval;
}

PyObject *
ColorTableAttributes_GetNumColorTables(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    return PyInt_FromLong((long)obj->data->GetColorTables().size());
}

PyObject *
ColorTableAttributes_AddColorTables(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    PyObject *element = NULL;
    if(!PyArg_ParseTuple(args, "O", &element))
        return NULL;
    if(!PyColorControlPointList_Check(element))
        return PyErr_Format(PyExc_TypeError, "expected attr object of type ColorControlPointList");
    ColorControlPointList *newData = PyColorControlPointList_FromPyObject(element);
    obj->data->AddColorTables(*newData);
    obj->data->SelectColorTables();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
ColorTableAttributes_Remove_One_ColorTables(PyObject *self, int index)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    // Remove in the AttributeGroupVector instead of calling RemoveColorTables() because we don't want to delete the object; just remove it.
    AttributeGroupVector &atts = obj->data->GetColorTables();
    AttributeGroupVector::iterator pos = atts.begin();
    // Iterate through the vector "index" times.
    for(int i = 0; i < index; ++i)
        ++pos;

    // If pos is still a valid iterator, remove that element.
    if(pos != atts.end())
    {
        // NOTE: Leak the object since other Python objects may reference it. Ideally,
        // we would put the object into some type of pool to be cleaned up later but
        // this will do for now.
        //
        // delete *pos;
        atts.erase(pos);
    }

    obj->data->SelectColorTables();
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *
ColorTableAttributes_RemoveColorTables(PyObject *self, PyObject *args)
{
    int index = -1;
    if(!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "Expecting integer index");
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    if(index < 0 || index >= obj->data->GetNumColorTables())
        return PyErr_Format(PyExc_IndexError, "Index out of range");

    return ColorTableAttributes_Remove_One_ColorTables(self, index);
}

PyObject *
ColorTableAttributes_ClearColorTables(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    int n = obj->data->GetNumColorTables();
    for(int i = 0; i < n; ++i)
    {
        ColorTableAttributes_Remove_One_ColorTables(self, 0);
        Py_DECREF(Py_None);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_SetDefaultContinuous(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the defaultContinuous in the object.
    obj->data->SetDefaultContinuous(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_GetDefaultContinuous(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetDefaultContinuous().c_str());
    return retval;
}

/*static*/ PyObject *
ColorTableAttributes_SetDefaultDiscrete(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the defaultDiscrete in the object.
    obj->data->SetDefaultDiscrete(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
ColorTableAttributes_GetDefaultDiscrete(PyObject *self, PyObject *args)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetDefaultDiscrete().c_str());
    return retval;
}



PyMethodDef PyColorTableAttributes_methods[COLORTABLEATTRIBUTES_NMETH] = {
    {"Notify", ColorTableAttributes_Notify, METH_VARARGS},
    {"SetColorTableNames", ColorTableAttributes_SetColorTableNames, METH_VARARGS},
    {"GetColorTableNames", ColorTableAttributes_GetColorTableNames, METH_VARARGS},
    {"SetColorTableActiveFlags", ColorTableAttributes_SetColorTableActiveFlags, METH_VARARGS},
    {"GetColorTableActiveFlags", ColorTableAttributes_GetColorTableActiveFlags, METH_VARARGS},
    {"GetColorTables", ColorTableAttributes_GetColorTables, METH_VARARGS},
    {"GetNumColorTables", ColorTableAttributes_GetNumColorTables, METH_VARARGS},
    {"AddColorTables", ColorTableAttributes_AddColorTables, METH_VARARGS},
    {"RemoveColorTables", ColorTableAttributes_RemoveColorTables, METH_VARARGS},
    {"ClearColorTables", ColorTableAttributes_ClearColorTables, METH_VARARGS},
    {"SetDefaultContinuous", ColorTableAttributes_SetDefaultContinuous, METH_VARARGS},
    {"GetDefaultContinuous", ColorTableAttributes_GetDefaultContinuous, METH_VARARGS},
    {"SetDefaultDiscrete", ColorTableAttributes_SetDefaultDiscrete, METH_VARARGS},
    {"GetDefaultDiscrete", ColorTableAttributes_GetDefaultDiscrete, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
ColorTableAttributes_dealloc(PyObject *v)
{
   ColorTableAttributesObject *obj = (ColorTableAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *ColorTableAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyColorTableAttributes_getattr(PyObject *self, char *name)
{
#include <visit-config.h>
    if(strcmp(name, "colorTableNames") == 0)
        return ColorTableAttributes_GetColorTableNames(self, NULL);
    if(strcmp(name, "colorTableActiveFlags") == 0)
        return ColorTableAttributes_GetColorTableActiveFlags(self, NULL);
    if(strcmp(name, "colorTables") == 0)
        return ColorTableAttributes_GetColorTables(self, NULL);
    if(strcmp(name, "defaultContinuous") == 0)
        return ColorTableAttributes_GetDefaultContinuous(self, NULL);
    if(strcmp(name, "defaultDiscrete") == 0)
        return ColorTableAttributes_GetDefaultDiscrete(self, NULL);

#if VISIT_OBSOLETE_AT_VERSION(3,5,0)
#error This code is obsolete in this version. Please remove it.
#else
    // Try and handle legacy fields in ColorTableAttributes

    //
    // Removed in 3.3.0
    //
    if(strcmp(name, "activeContinuous") == 0)
    {
        ColorTableAttributesObject *ColorTableObj = (ColorTableAttributesObject *)self;
        std::string defaultContinuous = ColorTableObj->data->GetDefaultContinuous();
        return PyString_FromString(defaultContinuous.c_str());
    }
    if(strcmp(name, "activeDiscrete") == 0)
    {
        ColorTableAttributesObject *ColorTableObj = (ColorTableAttributesObject *)self;
        std::string defaultDiscrete = ColorTableObj->data->GetDefaultDiscrete();
        return PyString_FromString(defaultDiscrete.c_str());
    }
#endif
#if VISIT_OBSOLETE_AT_VERSION(3,6,0)
#error This code is obsolete in this version. Please remove it.
#else
    // Try and handle legacy fields in ColorTableAttributes

    //
    // Removed in 3.4.0
    //
    if(strcmp(name, "taggingFlag") == 0)
    {
        PyErr_WarnEx(NULL,
                    "taggingFlag is no longer a valid Color Table "
                    "attribute.\nIt's value is being ignored, please remove "
                    "it from your script.\n", 3);
        return PyInt_FromLong(0L);
    }
#endif

    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyColorTableAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyColorTableAttributes_methods[i].ml_name),
                PyString_FromString(PyColorTableAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyColorTableAttributes_methods, self, name);
}

int
PyColorTableAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
#include <visit-config.h>
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "colorTableNames") == 0)
        obj = ColorTableAttributes_SetColorTableNames(self, args);
    else if(strcmp(name, "colorTableActiveFlags") == 0)
        obj = ColorTableAttributes_SetColorTableActiveFlags(self, args);
    else if(strcmp(name, "defaultContinuous") == 0)
        obj = ColorTableAttributes_SetDefaultContinuous(self, args);
    else if(strcmp(name, "defaultDiscrete") == 0)
        obj = ColorTableAttributes_SetDefaultDiscrete(self, args);

#if VISIT_OBSOLETE_AT_VERSION(3,5,0)
#error This code is obsolete in this version. Please remove it.
#else
   // Try and handle legacy fields in ColorTableAttributes
    if(obj == &NULL_PY_OBJ)
    {
        ColorTableAttributesObject *ColorTableObj = (ColorTableAttributesObject *)self;

        //
        // Removed in 3.3.0
        //
        if(strcmp(name, "activeContinuous") == 0)
        {
            const std::string defaultCont = PyString_AsString(args);
            PyErr_WarnEx(NULL, "'activeContinuous' is obsolete. Use 'defaultContinuous'.", 3);
            ColorTableObj->data->SetDefaultContinuous(defaultCont);
            Py_INCREF(Py_None);
            obj = Py_None;
        }
        if(strcmp(name, "activeDiscrete") == 0)
        {
            const std::string defaultDisc = PyString_AsString(args);
            PyErr_WarnEx(NULL, "'activeDiscrete' is obsolete. Use 'defaultDiscrete'.", 3);
            ColorTableObj->data->SetDefaultDiscrete(defaultDisc);
            Py_INCREF(Py_None);
            obj = Py_None;
        }
    }
#endif
#if VISIT_OBSOLETE_AT_VERSION(3,6,0)
#error This code is obsolete in this version. Please remove it.
#else
   // Try and handle legacy fields in ColorTableAttributes
    if(obj == &NULL_PY_OBJ)
    {
        //
        // Removed in 3.4.0
        //
        if(strcmp(name, "taggingFlag") == 0)
        {
            PyErr_WarnEx(NULL, "'taggingFlag' is obsolete. Tags are always enabled now.", 3);
            Py_INCREF(Py_None);
            obj = Py_None;
        }
    }
#endif
    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
ColorTableAttributes_print(PyObject *v, FILE *fp, int flags)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)v;
    fprintf(fp, "%s", PyColorTableAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
ColorTableAttributes_str(PyObject *v)
{
    ColorTableAttributesObject *obj = (ColorTableAttributesObject *)v;
    return PyString_FromString(PyColorTableAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *ColorTableAttributes_Purpose = "This class contains the list of colortables.";
#else
static char *ColorTableAttributes_Purpose = "This class contains the list of colortables.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(ColorTableAttributesType,         \
                  "ColorTableAttributes",           \
                  ColorTableAttributesObject,       \
                  ColorTableAttributes_dealloc,     \
                  ColorTableAttributes_print,       \
                  PyColorTableAttributes_getattr,   \
                  PyColorTableAttributes_setattr,   \
                  ColorTableAttributes_str,         \
                  ColorTableAttributes_Purpose,     \
                  ColorTableAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
ColorTableAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &ColorTableAttributesType
         || Py_TYPE(other) != &ColorTableAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    ColorTableAttributes *a = ((ColorTableAttributesObject *)self)->data;
    ColorTableAttributes *b = ((ColorTableAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static ColorTableAttributes *defaultAtts = 0;
static ColorTableAttributes *currentAtts = 0;

static PyObject *
NewColorTableAttributes(int useCurrent)
{
    ColorTableAttributesObject *newObject;
    newObject = PyObject_NEW(ColorTableAttributesObject, &ColorTableAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new ColorTableAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new ColorTableAttributes(*defaultAtts);
    else
        newObject->data = new ColorTableAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapColorTableAttributes(const ColorTableAttributes *attr)
{
    ColorTableAttributesObject *newObject;
    newObject = PyObject_NEW(ColorTableAttributesObject, &ColorTableAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (ColorTableAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
ColorTableAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewColorTableAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef ColorTableAttributesMethods[] = {
    {"ColorTableAttributes", ColorTableAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *ColorTableAttributesObserver = 0;

std::string
PyColorTableAttributes_GetLogString()
{
    std::string s("ColorTableAtts = ColorTableAttributes()\n");
    if(currentAtts != 0)
        s += PyColorTableAttributes_ToString(currentAtts, "ColorTableAtts.", true);
    return s;
}

static void
PyColorTableAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("ColorTableAtts = ColorTableAttributes()\n");
        s += PyColorTableAttributes_ToString(currentAtts, "ColorTableAtts.", true);
        cb(s);
    }
}

void
PyColorTableAttributes_StartUp(ColorTableAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyColorTableAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(ColorTableAttributesObserver == 0)
    {
        ColorTableAttributesObserver = new ObserverToCallback(subj,
            PyColorTableAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyColorTableAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete ColorTableAttributesObserver;
    ColorTableAttributesObserver = 0;
}

PyMethodDef *
PyColorTableAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return ColorTableAttributesMethods;
}

bool
PyColorTableAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &ColorTableAttributesType);
}

ColorTableAttributes *
PyColorTableAttributes_FromPyObject(PyObject *obj)
{
    ColorTableAttributesObject *obj2 = (ColorTableAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyColorTableAttributes_New()
{
    return NewColorTableAttributes(0);
}

PyObject *
PyColorTableAttributes_Wrap(const ColorTableAttributes *attr)
{
    return WrapColorTableAttributes(attr);
}

void
PyColorTableAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    ColorTableAttributesObject *obj2 = (ColorTableAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyColorTableAttributes_SetDefaults(const ColorTableAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new ColorTableAttributes(*atts);
}

